<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #Model {
        width: 800px;
        height: 600px;
        background-color: #ccc;
        padding: 10px;
        margin: auto;
        touch-action: none;
      }
    </style>
  </head>
  <body>
    <div id="Model"></div>
    <script src="https://cdn.bootcdn.net/ajax/libs/three.js/r128/three.js"></script>
    <script src="http://www.yanhuangxueyuan.com/threejs/examples/js/loaders/OBJLoader.js"></script>
    <script src="http://www.yanhuangxueyuan.com/threejs/examples/js/controls/OrbitControls.js"></script>
    <script src="http://www.yanhuangxueyuan.com/threejs/examples/js/loaders/GLTFLoader.js"></script>
    <script>
      
      

      // const textureLoader = new THREE.TextureLoader(manager);
      // const texture = textureLoader.load("./publi/uv_grid_opengl.jpg");

      const objLoader = (url, scene, material) => {
        const loader = new THREE.OBJLoader();
        var color = new THREE.Color("red");
        let obj = null;
        loader.load(url, function (object) {
          obj = object;
          console.log(obj);
          obj.position.z = -20;
          obj.position.y = 0;
          obj.position.x = 0;
          let obj1 = obj.children[0].material;
          for (let i = 0; i < obj.children[0].material.length; i++) {
            console.log(obj1[i]);
            if (obj1[i].id === 7) {
              obj1[i].color = color;
              obj1[i].transparent = true;
              obj1[i].opacity = 0.1;
              // obj1[i].pozition.z += 0.1
            }
          }
          scene.add(obj);
        });
        return obj;
      };
      // 创建场景对象
      function init(params) {
        const scene = new THREE.Scene();
        // 创建辅助坐标
        const axes = new THREE.AxesHelper(500);

        // 创建网格模型
        const geometry = new THREE.BoxGeometry(10, 10, 10);

        // 创建材质对象
        const material = new THREE.MeshLambertMaterial({
          color: 0x0000ff,
          transparent: true,
          opacity: 0.3,
        });

        // 创建网格模型对象mesh
        const mesh = new THREE.Mesh(geometry, material);
        // mesh.setPosition(10, 10, 10);
        mesh.position.z = 5;
        mesh.position.x = 5;
        mesh.position.y = 5;
        // scene.add(mesh);

        // 点光源
        const point = new THREE.PointLight(0xffffff);

        // 点光源位置
        point.position.set(40, 20, 30);

        // 添加点光源
        scene.add(point);

        // 创建环境光
        const ambient = new THREE.AmbientLight(0x444444);
        scene.add(ambient);

        // 相机设置
        const camera = new THREE.PerspectiveCamera(45, 1.5, 1, 1000);

        // 设置相机位置
        camera.position.set(50, 50, 50);

        camera.lookAt(scene.position);

        // 创建渲染器对象
        const renderer = new THREE.WebGLRenderer();
        const container = document.getElementById("Model");

        // 加载三维模型
        objLoader("./public/productline.obj", scene, material);

        function render() {
          requestAnimationFrame(render);
          //  执行渲染操作
          renderer.render(scene, camera);
        }
        renderer.setSize(800, 600);

        // 设置背景颜色
        renderer.setClearColor(0xb9d3ff, 0.2);

        container.appendChild(renderer.domElement);

        scene.add(axes);

        //相机控制器
        const controls = new THREE.OrbitControls(camera);
        render();
      }
      init();
    </script>
  </body>
</html>
